package com.itheima.app.controller;


import com.itheima.app.interceptor.UserHolder;
import com.itheima.app.manager.UserManager;

import com.itheima.domain.db.User;
import com.itheima.domain.db.UserInfo;

import com.itheima.vo.UserInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
public class UserController {

    @Autowired
    private UserManager userManager;

    //修改手机号- 1 发送短信验证码
    @PostMapping("/users/phone/sendVerificationCode")
    public void sendVerificationCode(){
        //调用manager查找到手机号
        String phone = userManager.findPhone();

        //发送手机验证码
        userManager.sendSmsCode(phone);
    }

    //修改手机号 - 2 校验验证码
    @PostMapping("/users/phone/checkVerificationCode")
    public Map<String,Boolean> checkVerificationCode(@RequestBody Map<String,String> map){
        //获取用户输入的验证码
        String verificationCode = map.get("verificationCode");

        return userManager.checkVerificationCode(verificationCode);
    }

    //修改手机号 - 3 保存
    @PostMapping("/users/phone")
    public void saveUserPhone(@RequestBody Map<String,String> map){
        //获取用户输入的手机号
        String phone = map.get("phone");

        userManager.saveUserPhone(phone);
    }

    //保存用户,返回主键
    @PostMapping("/save")
    public Long save(@RequestBody User user) {
        return userManager.save(user);
    }

    //根据手机号查询用户
    @GetMapping("/findByPhone")
    public User findByPhone(String phone) {
        return userManager.findByPhone(phone);
    }

    //发送手机验证码
    @PostMapping("/user/login")
    public void sendSmsCode(@RequestBody Map<String,String> map) {
        String phone = map.get("phone");

        userManager.sendSmsCode(phone);
    }

    //检验手机验证码
    @PostMapping("/user/loginVerification")
    public Map<String,Object> loginVerification(@RequestBody Map<String,String> map) {
        String phone = map.get("phone");
        String verificationCode = map.get("verificationCode");
        return  userManager.regAndLogin(phone, verificationCode);
    }

    //完善个人基本信息
    @PostMapping("/user/loginReginfo")
    public void saveUserBaseInfo(@RequestBody UserInfo userInfo) {
        User user = UserHolder.get();
        userInfo.setId(user.getId());
        userManager.saveUserBaseInfo(userInfo);
    }


    //完善个人头像信息
    @PostMapping({"/user/loginReginfo/head","/users/header"})
    public void saveUserHeadInfo(MultipartFile headPhoto) throws IOException {
        User user = UserHolder.get();
        Long id = user.getId();

        userManager.saveUserHeadInfo(id,headPhoto);

    }

    //查询个人信息
    @GetMapping("/users")
    public UserInfoVo findUserInfo(Long userID, Long huanxinID) {
        //查询
        if (userID != null) {
            return userManager.findById(userID);
        } else if (huanxinID != null) {
            return userManager.findById(huanxinID);
        } else {
            User user = UserHolder.get();
            return userManager.findById(user.getId());
        }
    }

    //更新个人信息
    @PutMapping("/users")
    public void updateUserBaseInfo(@RequestBody UserInfo userInfo) {
        userManager.updateUserBaseInfo(userInfo);
    }


    /*private User getUserFromToken(String token) {
        Map map = JwtUtil.parseToken(token);
        User user = BeanUtil.mapToBean(map, User.class, true);
        return user;
    }*/

}
